package com.wxiao.exam.controller.teacher;

import com.wxiao.exam.*;
import com.wxiao.exam.pojo.TeacherDO;
import com.wxiao.exam.pojo.vo.ExamVO;
import com.wxiao.exam.pojo.vo.QuestionManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/teacher/exam")
public class TeacherExamController {

    @Autowired
    ExamService examService;

    @Autowired
    QuestionService questionService;

    @Value("#{properties['exam.pageSize']}")
    private int pageSize;
    @Value("#{properties['exam.navigatePages']}")
    private int navigatePages;

   /* @Autowired
    QuestionService questionService;

    @Autowired
    ExaminationResultService examinationResultService;*/

    /**
     * 查询单选题无条件无页码
     */
    @RequestMapping("/list")
    public String list(ExamVO query, Model model) throws Exception {
        model.addAttribute("function", 2);
        query.setSize(navigatePages);
        query.setPageSize(pageSize);
        PageBean<ExamVO> pageBean = examService.listExam(query);
        model.addAttribute("pageBean", pageBean);
        return "teacher/exam_list";
    }

    //添加新的考试
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ExamVO examVO, HttpServletRequest request) throws Exception {
        examVO.setEndtime(new Date());//先随便设置
        examVO.setFkStatus(1);
        TeacherDO teacher = (TeacherDO) request.getSession().getAttribute("teacher");
        examVO.setFkTeacher(teacher.getId());
        examVO.setPoints(0);
        examVO.setSinglepoints(0);
        examVO.setMultipoints(0);
        examVO.setJudgepoints(0);
        return examService.addExam(examVO);
    }

    /**
     * 获取试卷列表
     *
     * @param eid
     * @param pageCode
     * @param model
     */
    @RequestMapping("/examManager/{eid}")
    public String examManager(@PathVariable Integer eid, Integer pageCode, Model model) {

        pageCode = DataUtils.getPageCode(pageCode + "");
        PageBean<QuestionManageVO> pageBean =
                questionService.listQuestionByExam(eid, pageCode, pageSize, navigatePages);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("examId", eid);
        return "teacher/question_manage";
    }


}
